package com.yanqu.road.dao.impl.activity.seacraft;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.seacraft.SeacraftSouthSeaCrossUserData;
import com.yanqu.road.entity.activity.seacraft.SeacraftSouthSeaUseSpeedUpInfo;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Description
 * @Author cwq
 * @Data 2022/2/13 17:16
 */
public class SeacraftSouthSeaCrossUserDataDaoImpl extends TempDataObjectDao<SeacraftSouthSeaCrossUserData> {
    public static String getSeacraftSouthSeaCrossUserDataSql = "select * from `t_c_activity_seacraft_southsea_user` where `activityId`=? ";

    public Map<Long, SeacraftSouthSeaCrossUserData> getSeacraftSouthSeaCrossUserDataMap(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, SeacraftSouthSeaCrossUserData> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getSeacraftSouthSeaCrossUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                SeacraftSouthSeaCrossUserData item = parse(rs);
                map.put(item.getUserId(), item);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateSeacraftSouthSeaCrossUserDataSql = "update `t_c_activity_seacraft_southsea_user` set serverId=?,unionId=?,score=?,raidAttackTimes=?,round=?,attackNum=?,shipMemberMap=?,donationShipMap=?,guessMap=?,cellInfoList=?,floor=?,gotBigReward=?,useSpeedUpInfoMap=? where activityId=? and userId=?";
    @Override
    protected boolean updateData(SeacraftSouthSeaCrossUserData seacraftSouthSeaCrossUserData) {
        seacraftSouthSeaCrossUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, seacraftSouthSeaCrossUserData.getServerId()));
        para.put(i++, new DbParameter(Types.VARCHAR, seacraftSouthSeaCrossUserData.getUnionId()));
        para.put(i++, new DbParameter(Types.INTEGER, seacraftSouthSeaCrossUserData.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, seacraftSouthSeaCrossUserData.getRaidAttackTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, seacraftSouthSeaCrossUserData.getRound()));
        para.put(i++, new DbParameter(Types.INTEGER, seacraftSouthSeaCrossUserData.getAttackNum()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(seacraftSouthSeaCrossUserData.getShipMemberMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(seacraftSouthSeaCrossUserData.getDonationShipMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(seacraftSouthSeaCrossUserData.getGuessMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(seacraftSouthSeaCrossUserData.getCellInfoList())));
        para.put(i++, new DbParameter(Types.INTEGER, seacraftSouthSeaCrossUserData.getFloor()));
        para.put(i++, new DbParameter(Types.INTEGER, seacraftSouthSeaCrossUserData.getGotBigReward()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(seacraftSouthSeaCrossUserData.getUseSpeedUpInfoMap())));
        para.put(i++, new DbParameter(Types.INTEGER, seacraftSouthSeaCrossUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, seacraftSouthSeaCrossUserData.getUserId()));
        if (getDb().executeNoneQuery(updateSeacraftSouthSeaCrossUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertSeacraftSouthSeaCrossUserDataSql = "insert into `t_c_activity_seacraft_southsea_user`(activityId,userId,serverId,unionId,score,raidAttackTimes,round,attackNum,shipMemberMap,donationShipMap,guessMap,cellInfoList,floor,gotBigReward,useSpeedUpInfoMap) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(SeacraftSouthSeaCrossUserData seacraftSouthSeaCrossUserData) {
        seacraftSouthSeaCrossUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, seacraftSouthSeaCrossUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, seacraftSouthSeaCrossUserData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, seacraftSouthSeaCrossUserData.getServerId()));
        para.put(i++, new DbParameter(Types.VARCHAR, seacraftSouthSeaCrossUserData.getUnionId()));
        para.put(i++, new DbParameter(Types.INTEGER, seacraftSouthSeaCrossUserData.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, seacraftSouthSeaCrossUserData.getRaidAttackTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, seacraftSouthSeaCrossUserData.getRound()));
        para.put(i++, new DbParameter(Types.INTEGER, seacraftSouthSeaCrossUserData.getAttackNum()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(seacraftSouthSeaCrossUserData.getShipMemberMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(seacraftSouthSeaCrossUserData.getDonationShipMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(seacraftSouthSeaCrossUserData.getGuessMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(seacraftSouthSeaCrossUserData.getCellInfoList())));
        para.put(i++, new DbParameter(Types.INTEGER, seacraftSouthSeaCrossUserData.getFloor()));
        para.put(i++, new DbParameter(Types.INTEGER, seacraftSouthSeaCrossUserData.getGotBigReward()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(seacraftSouthSeaCrossUserData.getUseSpeedUpInfoMap())));
        if (getDb().executeNoneQuery(insertSeacraftSouthSeaCrossUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected SeacraftSouthSeaCrossUserData parseData(ResultSet rs) throws SQLException {
        SeacraftSouthSeaCrossUserData seacraftSouthSeaCrossUserData = new SeacraftSouthSeaCrossUserData();
        seacraftSouthSeaCrossUserData.setActivityId(rs.getInt("activityId"));
        seacraftSouthSeaCrossUserData.setUserId(rs.getLong("userId"));
        seacraftSouthSeaCrossUserData.setServerId(rs.getLong("serverId"));
        seacraftSouthSeaCrossUserData.setUnionId(rs.getString("unionId"));
        seacraftSouthSeaCrossUserData.setScore(rs.getInt("score"));
        seacraftSouthSeaCrossUserData.setRaidAttackTimes(rs.getInt("raidAttackTimes"));
        seacraftSouthSeaCrossUserData.setRound(rs.getInt("round"));
        seacraftSouthSeaCrossUserData.setAttackNum(rs.getInt("attackNum"));
        seacraftSouthSeaCrossUserData.setShipMemberMap(JSON.parseObject(rs.getString("shipMemberMap"), new TypeReference<java.util.Map<java.lang.Integer, com.yanqu.road.entity.activity.seacraft.SeacraftSouthSeaTeamInfo>>() {}));
        seacraftSouthSeaCrossUserData.setDonationShipMap(JSON.parseObject(rs.getString("donationShipMap"), new TypeReference<java.util.Map<java.lang.Integer, java.lang.Long>>() {}));
        seacraftSouthSeaCrossUserData.setGuessMap(JSON.parseObject(rs.getString("guessMap"), new TypeReference<java.util.Map<java.lang.Integer, com.yanqu.road.entity.activity.seacraft.SeacraftSouthSeaGuessInfo>>() {}));
        List list = JSON.parseObject(rs.getString("cellInfoList"), new TypeReference<java.util.List<com.yanqu.road.entity.activity.seacraft.SeacraftSouthSeaCellInfo>>() {});
        if(list == null){
            // 因为字段运行为null 加个保护
            list = new ArrayList();
        }
        seacraftSouthSeaCrossUserData.setCellInfoList(list);
        seacraftSouthSeaCrossUserData.setFloor(rs.getInt("floor"));
        seacraftSouthSeaCrossUserData.setGotBigReward(rs.getInt("gotBigReward"));
        seacraftSouthSeaCrossUserData.setUseSpeedUpInfoMap(JSON.parseObject(rs.getString("useSpeedUpInfoMap"), new TypeReference<ConcurrentHashMap<Integer, SeacraftSouthSeaUseSpeedUpInfo>>() {}));

        return seacraftSouthSeaCrossUserData;
    }
}